package com.qiyun.dinghuohui.utils;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.v4.app.NotificationCompat;

/**
 * Created by xlm on 2018\5\25 0025
 */

public class NotificationUtils {
    /**
     * 创建通知，  * 请在调用此方法时开启子线程
     *
     * @param context 上下文
     * @param icon    通知图片
     * @param title   通知标题
     * @param content 通知主内容
     * @param intent  意图
     * @param id
     */
    public static void createNotif(Context context, int icon, String title, String content, Intent intent, int id) {
        //可以打开任意应用所在页
        PendingIntent pendingIntent = PendingIntent.getActivity(context, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            mBuilder.setContentTitle(title)
                    .setContentText(content)
                    .setContentIntent(pendingIntent)
                    .setWhen(System.currentTimeMillis())
                    .setPriority(Notification.PRIORITY_MAX)
                    .setOngoing(false)
                    .setAutoCancel(true)
                    .setDefaults(Notification.DEFAULT_ALL)//静音或者震动跟随系统
                    .setSmallIcon(icon);
        }
        Notification notification = mBuilder.build();
        notification.flags = Notification.FLAG_ONGOING_EVENT;
        notification.flags = Notification.FLAG_AUTO_CANCEL;
        if (notificationManager != null) {
            notificationManager.notify(id, notification);
        }
    }
}
